// Decompiled by DJ v2.3.3.38 Copyright 2000 Atanas Neshkov  Date: 03/11/2007 6:40:44
// Home Page : http//members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   RRegistroDao.java

package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.RelacionRegistrosDTO;
import ig.formato.util.IGFormatoException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

public class RRegistroDao extends DAO {

    public RRegistroDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDREGISTROPADRE, IDREGISTROHIJO, CAMPOPADRE, CAMPOHIJO FROM R_REGISTRO_REGISTRO WHERE IDREGISTROPADRE = ?");
            prep.setInt(1, id.intValue());
            for (resul = prep.executeQuery(); resul.next(); c.add(getDatos(resul, new RelacionRegistrosDTO()))) {
                ;
            }

            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, IDREGISTROPADRE, IDREGISTROHIJO, CAMPOPADRE, CAMPOHIJO FROM R_REGISTRO_REGISTRO");

            for (resul = prep.executeQuery(); resul.next(); c.add(getDatos(resul, new RelacionRegistrosDTO()))) {
                ;
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        RelacionRegistrosDTO dto = (RelacionRegistrosDTO) obj;
        try {
            /*connectionIgFormato = */            getConnection();
            if (dto.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO R_REGISTRO_REGISTRO(IDREGISTROPADRE, IDREGISTROHIJO, CAMPOPADRE, CAMPOHIJO, ID) VALUES(?, ?, ?, ?, ?)");
                dto.setId(getMaximoId("R_REGISTRO_REGISTRO"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE R_REGISTRO_REGISTRO SET IDREGISTROPADRE=?, IDREGISTROHIJO=?, CAMPOPADRE=?, CAMPOHIJO=? WHERE ID=?");
            }
            setDatos(prep, dto,
                    "IDREGISTROPADRE, IDREGISTROHIJO, CAMPOPADRE, CAMPOHIJO, ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM R_REGISTRO_REGISTRO WHERE IDREGISTROPADRE = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
            prep.close();
            prep = connectionIgFormato.prepareStatement("DELETE FROM R_REGISTRO_REGISTRO WHERE IDREGISTROHIJO = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            /*connectionIgFormato = */            getConnection();
            Integer data[] = (Integer[]) (Integer[]) obj;
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM R_REGISTRO_REGISTRO WHERE IDREGISTROPADRE=? AND IDREGISTROHIJO =? AND CAMPOPADRE =? AND CAMPOHIJO=?");
            prep.setInt(1, data[0].intValue());
            prep.setInt(2, data[1].intValue());
            prep.setInt(3, data[2].intValue());
            prep.setInt(4, data[3].intValue());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return Boolean.valueOf(false);
    }
}
